import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubNotice extends ViewBase {

    private btnFix: cc.Node;
    private isFixing: boolean = false
    private txtNotice: cc.Label;
    private btnSubmit: cc.Node;
    private txtInputNotice: cc.EditBox;

    onEnable(): void {
        super.onEnable()
        this.btnFix = this.findChild(this.node, "btnFix")
        UIHelper.onAddEvent(this.btnFix, () => {
            this.isFixing = true;
            this.refresh()
        })
        this.txtNotice = this.getChild(this.node, "txtNotice", cc.Label)
        this.btnSubmit = this.findChild(this.node, "btnSubmit")
        this.txtInputNotice = this.getChild(this.node, "txtInputNotice", cc.EditBox)
        UIHelper.onAddEvent(this.btnSubmit, () => {
            if (this.txtInputNotice.string == "") {
                UIHelper.showTip("请输入公告")
                return;
            }
            ClubSocketClient.req_club_fix_notice(UserModel.instance.nowClub.clubid, this.txtInputNotice.string, () => {
                this.isFixing = false
                this.refresh()
            })
        })
        this.refresh()
    }

    private refresh() {
        if(UserModel.instance.nowClub.message){
            this.txtInputNotice.string = UserModel.instance.nowClub.message + ""
            this.txtNotice.string = UserModel.instance.nowClub.message + ''
        }else{

        }
        this.btnSubmit.active = this.isFixing
        this.btnFix.active = !this.isFixing
        this.txtInputNotice.node.active = this.isFixing;
        this.txtNotice.node.active = !this.isFixing;
        if (UserModel.instance.nowClub.actor != ClubConfig.Club_Actor_Type.owner) {
            this.btnFix.active = false
        }
    }
}
